﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace __AdvanceWars
{
    class Level
    {
        int[,] map = new int[0,0];
        String[] dem;
        int widthi, heighti;
        public List<Texture2D> tileTextures = new List<Texture2D>();
        public int Width { get { return map.GetLength(1); }  }
        public int Height { get { return map.GetLength(0); }  }
        public void LoadLevel(String name)
        {
            TextReader te = new StreamReader(name);
            string rawdem = te.ReadLine();
            dem = rawdem.Split(',');
            widthi = Convert.ToInt32(dem[0]);
            heighti = Convert.ToInt32(dem[1]);
            map = new int[heighti, widthi];
            for (int i = 0; i < heighti; i++)
            {
                string line = te.ReadLine();
                for (int j = 0; j < widthi; j++)
                {
                    map[i, j] = (int)(line[j] - '0');
                }
            }
        }
        public void AddTexture(Texture2D texture)
        {
            tileTextures.Add(texture);
           
        }
        public void Draw(SpriteBatch batch)
        {
            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    int textureindex = map[y, x];
                    if (textureindex == -1)
                        continue;
                    Texture2D texture = tileTextures[textureindex];
                    batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
                }
            }
        }
    }
}
